package org.sp.controller;

import com.alibaba.fastjson.JSON;
import org.sp.entity.User;
import org.sp.service.ITestUserService;
import org.sp.service.impl.TestUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 76986
 */
@WebServlet("/user/delete")
public class DeleteUserController extends HttpServlet {
    private ITestUserService service = new TestUserServiceImpl();

    /**
     * 删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
//        request.getParameter()
        String id = request.getParameter("id");

        PrintWriter writer = response.getWriter();
        Map<String,Object> map = new ConcurrentHashMap<>(2);
        if (null == id || "".equals(id)){
            request.setAttribute("msg","参数错误");
            request.getRequestDispatcher("/user.jsp").forward(request,response);
            return;
        }
        Boolean flag = service.deleteUserById(Integer.parseInt(id));
        System.out.println(flag);
        if (flag){
            request.getSession().setAttribute("msg","删除成功");
        }else {
            request.getSession().setAttribute("msg","删除失败");
        }
        response.sendRedirect(request.getContextPath()+"/");
    }
}
